package com.huipinche.view.impl;

import com.huipinche.view.LoginPageView;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

import com.huipinche.view.utils.*;
import com.huipinche.service.impl.*;

public class LoginPageViewimpl implements LoginPageView {
    private JFrame loginFrame;
    private int page;
    final int WIDTH = 500;
    final int HEIGHT = 400;
    private boolean flag = true;



 /*   public static void main(String[] args) {
        LoginPageViewimpl login = new  LoginPageViewimpl();
        try{
            login.initialize();
        }catch (Exception e ) {
            e.printStackTrace();
        }
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        //  login.loginFrame.setVisible(true);
    }*/



    private void initialize() throws Exception {

        loginFrame = new JFrame("登录");

        loginFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        JTextArea sloganArea = new JTextArea("欢迎注册惠拼车\n 今天拼车人，明日人上人",
                3, 25);

        sloganArea.setOpaque(false);//设置文本域为透明
        sloganArea.setForeground(Color.white);//设置字体颜色
        sloganArea.setFont(new Font("楷体",Font.BOLD,30));//设置字体大小
        sloganArea.setEditable(false);//设置文本域不能被更改

        loginFrame.setIconImage(ImageIO.read(new File(PathUtils.getRealPath("logo3.jpeg"))));



        Image image=new ImageIcon(PathUtils.getRealPath("background4.png")).getImage();
        BackGroundPanel bgPanel = new BackGroundPanel(image);
        bgPanel.setBounds(0,0,WIDTH,HEIGHT);

        //组装登录相关元素
        /*BackGroundPanel bgPanel = new BackGroundPanel();
        bgPane  l.setBounds(0,0,WIDTH,HEIGHT);*/
        Box vBox = Box.createVerticalBox();

        //组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel accountLabel = new JLabel("用户名: ");
        accountLabel.setFont(new Font("楷体",Font.BOLD,18));//设置字体大小
        accountLabel.setForeground(Color.white);//设置字体颜色

        JTextField username = new JTextField();
        username.setPreferredSize(new Dimension (200,30));//设置宽高
        //username.setOpaque(false);

        uBox.add(accountLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(username);

        //组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("密   码:");
        passwordLabel.setFont(new Font("楷体",Font.BOLD,18));
        passwordLabel.setForeground(Color.white);

        JPasswordField passwordField =  new JPasswordField("",5);
        passwordField.setPreferredSize(new Dimension (200,30));
        //passwordField.setOpaque(false);

        pBox.add(passwordLabel);
        pBox.add(Box.createHorizontalStrut(20));
        pBox.add(passwordField);

        //组装功能键
        Box wBox = Box.createHorizontalBox();
        JCheckBox rememberBox = new JCheckBox("记住密码");
        //JTextField rememberField = new JTextField("记住密码",10);
        JCheckBox autoBox = new JCheckBox("自动登录");
        // JTextField autoField = new JTextField("自动登录",10);
        JButton forgetButton = new JButton("忘记密码");

        wBox.add(rememberBox);
        wBox.add(Box.createHorizontalStrut(40));
        wBox.add(autoBox);
        wBox.add(Box.createHorizontalStrut(40));
        wBox.add(forgetButton);

        //JButton btLogin,btExit;
        Icon login = new ImageIcon((PathUtils.getRealPath("car3.png")));
        Icon exit = new ImageIcon((PathUtils.getRealPath("car2.png")));


        // JTextField out = new JTextField("登录状态",25);

        JButton loginButton = new JButton("登录");
        loginButton.setBorderPainted(true);//设置透明度
        loginButton.setIcon(login);

        LoginPageViewimpl temp = this;
//        System.out.println("00"+ username.getText());
//        System.out.println("00"+ passwordField.getText());
//        System.out.println("11111");
        loginButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(new loginUserServiceimpl().loginUserService
                        (username.getText(),passwordField.getText())) {
                    new GeneralPageimpl("登录成功");
                    temp.page = 200;//homepage;
                    temp.flag = false;
                }else {
//                    System.out.println("获取的"+ username.getText());
//                    System.out.println("获取的"+ passwordField.getText());
                   new GeneralPageimpl("用户名或者密码错误"); //用户名或者密码错误
                }
            }
        });

        JButton registrationButton = new JButton("注册");
        registrationButton.setIcon(exit);
        registrationButton.setBorderPainted(false);//设置按钮透明度
        registrationButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                temp.page = 120;//register page;
                temp.flag = false;
            }
        });

        Box btnBox = Box.createHorizontalBox();

        btnBox.add(loginButton);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(registrationButton);


        vBox.add(sloganArea);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(wBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(btnBox);


        bgPanel.add(vBox);

       // System.out.println(9898989);

        loginFrame.add(bgPanel);
        loginFrame.setVisible(true);
        loginFrame.setResizable(false);


    }
    @Override
    public int LoginPage(boolean isleep) throws Exception {
        this.flag = isleep;
            initialize();
        while(this.flag){
            Thread.sleep(200);
        };
        switch (page){
            case 120:
                loginFrame.removeNotify();
                return 120;//register page
            case 200:
                loginFrame.removeNotify();
                return 200;//homepage
        }
        return 0;
    }

}
